package com.vast.admin.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.vast.admin.annotation.UseToken;
import com.vast.admin.entity.Lawsuit;
import com.vast.admin.entity.dto.StatisticsContracts;
import com.vast.admin.entity.statistics.MyContractStatisticsBean;
import com.vast.admin.entity.statistics.StatisticsByCompanyBean;
import com.vast.admin.service.ContractService;
import com.vast.admin.service.CustomerService;
import com.vast.admin.service.StatisticsService;
import com.vast.admin.service.impl.CompanyService;
import com.vast.admin.service.impl.LawsuitService;
import com.vast.common.core.domain.entity.SysUser;
import com.vast.common.utils.SecurityUtils;
import com.vast.common.constant.MessageCode;
import com.vast.common.core.domain.Result;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 统计相关接口 前端控制器
 * </p>
 *
 * @author zhaohao
 * @since 2018-10-24
 */
@RestController
@Api(value = "统计相关的api", tags = { "统计服务模块" })
@RequestMapping("/manager/statistics")
public class StatisticsController {

    @Autowired
    private CompanyService companyService;

    @Autowired
    private CustomerService customerService;

    @Autowired
    private ContractService contractService;

    @Autowired
    private LawsuitService lawsuitService;

    @Autowired
    private StatisticsService statisticsService;

    @ApiOperation(value = "合同统计", notes = "合同统计")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "登录获取的token", dataType = "String", required = true)

    })
    @UseToken
    @GetMapping("/getContractStatistics")
    public Result getContractStatistics() {
		SysUser manager = SecurityUtils.getLoginUser().getUser();
        if(manager != null) {
            StatisticsContracts statisticsContracts = contractService.getContractStatistics(manager);
            if(statisticsContracts != null){
                return new Result(MessageCode.SUCCESS, "获取成功", statisticsContracts);
            }else{
                return new Result(MessageCode.ERROR, "获取失败");
            }
        }else {
            return new Result(MessageCode.PARAM_ERROR, "参数错误");
        }
    }

    @ApiOperation(value = "获取合同完成前n企业", notes = "获取合同完成前n企业")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "登录获取的token", dataType = "String", required = true),
            @ApiImplicitParam(paramType = "query", name = "num", value = "前n数", dataType = "int", defaultValue = "10", required = true)

    })
    @UseToken
    @GetMapping("/getContractCompleteTopNum")
    public Result getContractCompleteTopNum(int num) {
		SysUser manager = SecurityUtils.getLoginUser().getUser();
        if(manager != null) {
            List<StatisticsContracts> statisticsContracts = contractService.getContractCompleteTopNum(num);
            return new Result(MessageCode.SUCCESS, "获取成功", statisticsContracts);
        }else {
            return new Result(MessageCode.PARAM_ERROR, "参数错误");
        }
    }

    @ApiOperation(value = "根据时间段获取合同签署情况", notes = "根据时间段获取合同签署情况")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "登录获取的token", dataType = "String", required = true),
            @ApiImplicitParam(paramType = "query", name = "startDate", value = "开始时间", dataType = "String", required = true),
            @ApiImplicitParam(paramType = "query", name = "endDate", value = "结束时间", dataType = "String", required = true)

    })
    @UseToken
    @GetMapping("/getNearDateContractRecordsData")
    public Result getNearDateContractRecordsData(String startDate, String endDate) {
		SysUser manager = SecurityUtils.getLoginUser().getUser();
        if(manager != null) {
            List<StatisticsContracts> statisticsContracts = contractService.getNearDateContractRecordsData(startDate,endDate);
            return new Result(MessageCode.SUCCESS, "获取成功", statisticsContracts);
        }else {
            return new Result(MessageCode.PARAM_ERROR, "参数错误");
        }
    }

    @ApiOperation(value = "获取企业合同签署情况统计", notes = "获取企业合同签署情况统计")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "登录获取的token", dataType = "String", required = true),
            @ApiImplicitParam(paramType = "query", name = "size", value = "分页条数", dataType = "int", defaultValue = "10", required = false),
            @ApiImplicitParam(paramType = "query", name = "current", value = "页码,起始值:1", dataType = "int", defaultValue = "1", required = false),
            @ApiImplicitParam(paramType = "query", name = "listMode", value = "是否集合模式:true不进行分页直接返回所有结果集", dataType = "boolean", defaultValue = "false", required = false),
            @ApiImplicitParam(paramType = "query", name = "companyId", value = "企业Id",  dataType = "Long", required = false)
    })
    @UseToken
    @GetMapping("/getContractStatisticsByCompany")
    public Result getContractStatisticsByCompany(@ApiParam(hidden = true) Page<StatisticsContracts> page, boolean listMode,Long companyId) {
		SysUser manager = SecurityUtils.getLoginUser().getUser();
    	if(manager != null) {
            if (listMode) {
                // size 小于 0 不在查询 total 及分页，自动调整为列表模式。
                // 注意！！这个地方自己控制好！！
                page.setSize(-1);
            }
            IPage<StatisticsContracts> statisticsContracts = contractService.getContractStatisticsByCompany(page,manager,companyId);
            return new Result(MessageCode.SUCCESS, "获取成功", statisticsContracts);
        }else {
            return new Result(MessageCode.PARAM_ERROR, "参数错误");
        }
    }


    @ApiOperation(value = "获取诉讼总数量", notes = "获取诉讼总数量")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "登录获取的token", dataType = "String", required = true)

    })
    @UseToken
    @GetMapping("/getLawsuitTotal")
    public Result getLawsuitTotal() {
		SysUser manager = SecurityUtils.getLoginUser().getUser();
        if(manager != null) {
            int total = lawsuitService.count();
            return new Result(MessageCode.SUCCESS, "获取成功", total);
        }else {
            return new Result(MessageCode.PARAM_ERROR, "参数错误");
        }
    }


    @ApiOperation(value = "获取即将到期诉讼数据", notes = "获取即将到期诉讼数据")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "登录获取的token", dataType = "String", required = true),
            @ApiImplicitParam(paramType = "query", name = "size", value = "分页条数", dataType = "int", defaultValue = "10", required = false),
            @ApiImplicitParam(paramType = "query", name = "current", value = "页码,起始值:1", dataType = "int", defaultValue = "1", required = false),
            @ApiImplicitParam(paramType = "query", name = "listMode", value = "是否集合模式:true不进行分页直接返回所有结果集", dataType = "boolean", defaultValue = "false", required = false),
            @ApiImplicitParam(paramType = "query", name = "days", value = "即将到期n天数", dataType = "int", defaultValue = "3", required = true)

    })
    @UseToken
    @GetMapping("/getLawsuitDueByDays")
    public Result getLawsuitDueByDays(@ApiParam(hidden = true) Page<Lawsuit> page, boolean listMode, int days) {
		SysUser manager = SecurityUtils.getLoginUser().getUser();
        if(manager != null && days > 0) {
            if (listMode) {
                // size 小于 0 不在查询 total 及分页，自动调整为列表模式。
                // 注意！！这个地方自己控制好！！
                page.setSize(-1);
            }
            IPage<Lawsuit> lawsuitList = lawsuitService.getLawsuitDueByDays(page,days);
            return new Result(MessageCode.SUCCESS, "获取成功", lawsuitList);
        }else {
            return new Result(MessageCode.PARAM_ERROR, "参数错误");
        }
    }


    @ApiOperation(value = "获取企业总数量", notes = "获取企业总数量")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "登录获取的token", dataType = "String", required = true)

    })
    @UseToken
    @GetMapping("/getCompanyTotal")
    public Result getCompanyTotal() {
		SysUser manager = SecurityUtils.getLoginUser().getUser();
        if(manager != null) {
            int total = companyService.count();
            return new Result(MessageCode.SUCCESS, "获取成功", total);
        }else {
            return new Result(MessageCode.PARAM_ERROR, "参数错误");
        }
    }


    @ApiOperation(value = "我的合同统计", notes = "我的合同统计")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "登录获取的token", dataType = "String", required = true),
            @ApiImplicitParam(paramType = "header", name = "page", value = "分页参数", dataType = "Object", required = true),
            @ApiImplicitParam(paramType = "header", name = "queryItems", value = "查询参数")

    })
    @UseToken
    @GetMapping("/myContractStatisticsPage")
    public Result<IPage<MyContractStatisticsBean>> myContractStatisticsPage(Page<MyContractStatisticsBean> page, String queryItems) {
		SysUser manager = SecurityUtils.getLoginUser().getUser();
        return new Result<>(MessageCode.SUCCESS, contractService.myContractStatisticsPage(manager, page, queryItems));
    }

    @ApiOperation(value = "合同信息统计", notes = "合同信息统计")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "登录获取的token", dataType = "String", required = true),
            @ApiImplicitParam(paramType = "header", name = "page", value = "分页参数", dataType = "Object", required = true),
            @ApiImplicitParam(paramType = "header", name = "queryItems", value = "查询参数")

    })
    @UseToken
    @GetMapping("/contractInfoStatisticsPage")
    public Result<IPage<MyContractStatisticsBean>> contractInfoStatisticsPage(Page<MyContractStatisticsBean> page, MyContractStatisticsBean contract) {
    	return new Result<IPage<MyContractStatisticsBean>>(MessageCode.SUCCESS, statisticsService.contractInfoStatisticsPage(page, contract));
    }

    @ApiOperation(value = "导出我的合同", notes = "导出我的合同")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "登录获取的token", dataType = "String", required = true),
            @ApiImplicitParam(paramType = "header", name = "queryItems", value = "查询参数")

    })
    @UseToken
    @GetMapping("/exportMyContractStatistics")
    public Result<List<MyContractStatisticsBean>> exportMyContractStatistics(String queryItems) {
		SysUser manager = SecurityUtils.getLoginUser().getUser();
        return new Result<>(MessageCode.SUCCESS, contractService.exportMyContractStatistics(manager, queryItems));
    }

    @ApiOperation(value = "导出合同信息", notes = "导出合同信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "token", value = "登录获取的token", dataType = "String", required = true),
            @ApiImplicitParam(paramType = "header", name = "queryItems", value = "查询参数")

    })
    @UseToken
    @GetMapping("/exportContractInfoStatistics")
    public Result<List<MyContractStatisticsBean>> exportContractInfoStatistics( Long templateRecordId, String startDate, String endDate) {
		SysUser manager = SecurityUtils.getLoginUser().getUser();
    	return new Result<>(MessageCode.SUCCESS, contractService.exportContractInfoStatistics(manager, templateRecordId, startDate, endDate));
    }

    @ApiOperation(value = "企业合同统计", notes = "企业合同统计")
    @GetMapping("/statisticsContractByCompany")
    public Result<IPage<StatisticsByCompanyBean>> statisticsContractByCompany(Page<StatisticsByCompanyBean> page, StatisticsByCompanyBean statistics){
        return new Result<>(MessageCode.SUCCESS, statisticsService.statisticsContractByCompany(page, statistics));
    }

    @ApiOperation(value = "部门合同统计", notes = "部门合同统计")
    @GetMapping("/statisticsContractByDept")
    public Result<IPage<StatisticsByCompanyBean>> statisticsContractByDept(Page<StatisticsByCompanyBean> page, StatisticsByCompanyBean statistics){
        return new Result<>(MessageCode.SUCCESS, statisticsService.statisticsContractByDept(page, statistics));
    }

}
